<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <script type="text/javascript" src="../../inc/ctx.jsp"></script>
        <script type="text/javascript" src="../js/g.js"></script>
        <script type="text/javascript" src="../js/channeltype.js"></script>
        <link href="../css/com.css" type="text/css" rel="stylesheet"/>
        <style>
            #datalist tr td{text-align:center;}
            #datalist tr td.red{color:red;}
        </style>
    </head>
    <body>
        <div id="panel">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr class="trhd">
                    <td width="10%"><h3 class="s1">栏目分类管理</h3></td>
                    <td width="13%">&nbsp;</td>
                    <td width="14%">&nbsp;</td>
                    <td colspan="3">(注意：请合理设置类别,类目总数不得超过1024个,当前最多可建立5级分类.)</td>
                </tr>
                <tr>
                    <td>板块类别：</td>
                    <td>
                        <script>out_select();</script></td>
                    <td>栏目状态：</td>
                    <td width="8%">
                        <label for="isopen"><span class="r">
                                <select name="isopen" id="isopen" tabindex="2">
                                    <option value=""></option>
                                    <option value="0">关闭</option>
                                    <option value="1">开放</option>
                                </select>
                            </span></label></td>
                    <td width="12%" class="l"> 类目总数为：<span id="catalogs_num"></span></td>
                    <td width="43%" class="r">
                        <input onClick="CAT.list(0);" name="submitBtn" type="button" class="button small orange" id="submitBtn" value="Φ 查询">
                        <input onclick="CAT.add(0, '0')" name="addBtn" type="button" class="button small orange" id="addBtn" value="＋ 增加主分类">
                        <input onclick="CAT.list(0);" name="backBtn" type="button" class="button small orange" id="backBtn" value="→ 返回主类表">
                        <input onclick="CAT.rebuildLevel();" name="rebuildLevelBtn" type="button" class="button small gray" id="rebuildLevelBtn" value="重建类目级别">

                    </td>
                </tr>
            </table>
        </div>


        <div class="databox">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <th width="4%">CID</th>
                    <th width="18%">分类名称</th>
                    <th width="6%">RID</th>
                    <th width="10%">节点数</th>
                    <th width="10%">排序号</th>
                    <th width="12%">CTP</th>
                    <th width="30%">操作</th>
                </tr>
                <tbody class="tbchg" id="datalist"></tbody>
            </table>
        </div>
        <div class="page" id="page"></div>

        <div style="display:block;clear:both;width:98%;height:50px;">&nbsp;</div>





        <!-- 数据处理 -->
        <script id="listTpl" type="text/html">
            {%for(var k in list){var e = list[k]; %}
        <tr>
            <td>{%=e.cid%}</td>
            <td class="{% if(e.isopen===0){ %}red{% } %}">{%=e.cname%}</td>
            <td>{%=e.rid%}</td>
            <td>{%=e.nodenum%}</td>
            <td>
                <input type="text" value="{%=e.ordnum%}" onchange="CAT.modord('{%=e.cid%}',this.value);" style="width:40px;" maxlength="4"/>
            </td>
            <td>{%=e.ctp%}</td>
            <td>
                <a href="javascript:;" onclick="CAT.modname('{%=e.cid%}','{%=e.cname%}');">修改名称</a>
                {% if(e.ctp.substring(0,e.ctp.length-1).indexOf('-')>0){ %}                    
                &nbsp;&nbsp;&nbsp;
                <a href="javascript:;" onclick="CAT.prelist('{%=e.ctp%}');">上级分类</a>
                {% } %}
                &nbsp;&nbsp;&nbsp;
                {% if(e.nodenum>0){ %}                  
                <a href="javascript:;" onclick="CAT.list('{%=e.cid%}');">下级分类</a>
                {% }else{ %}
                <a href="javascript:;" onclick="CAT.del('{%=e.cid%}','{%=e.rid%}');">删除</a>
                {% } %}
                &nbsp;&nbsp;&nbsp;
                <a href="javascript:;" onclick="CAT.add('{%=e.cid%}','{%=e.ctp%}');">添加分类</a>
                &nbsp;&nbsp;&nbsp;
                {% if(e.isopen==1){ %}
                <a href="javascript:;" onclick="CAT.swapopen('{%=e.cid%}','0');">关闭</a>
                {% }else{ %}
                <a href="javascript:;" onclick="CAT.swapopen('{%=e.cid%}','1');">开放</a>
                {% } %}
            </td>
        </tr>
        {%}%}
    </script>

    <script type="text/javascript">
        var crid;
        var $lsout = $('#datalist');
        var $num = $('#catalogs_num');
        var CAT={
            list:function(rid){
                crid= rid;
                var t = $('#infotype').val();
                var o = $('#isopen').val();
                if(va.isnull(t)){ut.m('请先选择板块类别,再查询分类.',3000);return;}
                var p={
                    rid:rid,infotype:t,isopen:o,ordnum:'asc'
                };
                $.ajax({type:"GET",cache:false,url:'../app/catalogs/list_factor.shtm',data:$.param(p),
                    success:function(x){
                        $lsout.html(template.render('listTpl',x));
                    },
                    complete:function(){},
                    error:function(x,s,e){ut.m(e);}
                });
            },
            prelist:function(ctp){
                var ca =  ctp.substring(0,ctp.length-1).split('-');
                var gocid = '0';
                if(ca.length>=3){
                    gocid = ca[ca.length-3];
                }
                CAT.list(gocid);
            },
            add:function(rid,ctp){
                var type = $('#infotype').val();
                var lev = ctp.substring(0,ctp.length-1).split('-').length;
                if(lev>=5){
                    ut.m('当前只能建立5级分类');
                    return;
                }
                if(va.isnull(type)){ut.m('请先选择版块类别.');return;}
                TxBox.winpop(420, 160, '添加新栏目', 'catalogs_add_pop.html?rid='+rid+'&ctp='+ctp+'&infotype='+type+'&t='+ut.r());
            },
            modord:function(cid,num){
                $.get('../app/catalogs/update_base.shtm?t='+ut.r(), "cid="+cid+"&ordnum="+num, function(x){
                    var r = $.trim(x);
                    if(r=='ok'){
                        CAT.list(crid);
                    }else{
                        ut.m('操作失败. '+r);
                    }
                });
            },
            modname:function(cid,title){
                var tit = window.prompt('当前类别: ID='+cid+','+title+'. 请输入新类名称(60个字符内):',title);
                if(va.isnull(tit)){return;}
                $.get('../app/catalogs/update_base.shtm?t='+ut.r(), "cid="+cid+"&cname="+ut.enc(tit), function(x){
                    var r = $.trim(x);
                    if(r=='ok'){
                        CAT.list(crid);
                    }else{
                        ut.m('修改类名称失败. '+r);
                    } 
                });
            },
            del:function(cid, rid){
                if(!window.confirm("确定执行分类删除么?该操作将删除关联资讯的数据"))return;
                $.get('../app/catalogs/delete.shtm?t='+ut.r(), "cid="+cid+"&rid="+rid, function(x){
                    var r = $.trim(x);
                    if(r=='ok'){
                        ut.m('删除成功.');
                        CAT.list(crid);
                    }else{
                        ut.m('删除失败. '+r);
                    }
                });
            },
            swapopen:function(cid,o){
                $.get('../app/catalogs/update_base.shtm?t='+ut.r(), "cid="+cid+"&isopen="+o, function(x){
                    var r = $.trim(x);
                    if(r=='ok'){
                        CAT.list(crid);
                    }else{
                        ut.m('操作失败. '+r);
                    }
                }); 
            },
            loadCatalogsNum:function(){
                $.get('../app/catalogs/catalogs_num.shtm?t='+ut.r(), function(x){
                    var r = $.trim(x);
                    $num.text(r);
                }); 
            },
            rebuildLevel:function(){
                if(!window.confirm("确定重建分类级别表么?注意：重建前会清空级别表数据."))return;
                TxBox.wait('正在清空类目级别表数据,请稍后...');
                $.get('../app/catalogslevel/delete_all.shtm?t='+ut.r(), function(x){
                    TxBox.changeWaitContent('已删除类目级别数据:'+$.trim(x)+' 条. 3s 后开始重建...',3000,
                    CAT._rebuildCatalogsLevel);
                });
            },
            _rebuildCatalogsLevel:function(){
                TxBox.changeWaitContent('正在重建类目级别数据,请稍后...');
                $.get('../app/catalogslevel/rebuild_catalogs_level.shtm?t='+ut.r(), function(x){
                    var r = $.trim(x);
                    TxBox.changeWaitContent('已重建记录数为: '+r,3000,TxBox.hide);
                }); 
            }
        };
        jQuery(function(){$('#infotype').val('0');CAT.loadCatalogsNum();});
        window.setInterval(CAT.loadCatalogsNum, 15000);
    </script>
</body>
</html>
